package com.example.lock.乐观锁.cas.verion_cas;
import java.util.concurrent.atomic.AtomicInteger;

public class OptimThread {

    public AtomicInteger version = new AtomicInteger();

    public static int a  = 0;


    public void update(int current,int update) {
       if(version.compareAndSet(current,update)){
           a = a + 1;
           version.decrementAndGet();
           //System.out.println("current:"+current+",update:"+update);
        }else {
           System.out.println("自旋失败");
       }

        //AtomicStampedReference asr = new AtomicStampedReference<>();
        //Unsafe unsafe = Unsafe.getUnsafe();
        /*if(a==0){
           a = a +1;
        }*/

    }

    public static void main(String[] args) throws Exception{
       OptimThread thread = new OptimThread();
        for(int i=0;i<10000;i++){
           new Thread(()->{
               thread.update(0,1);
           }).start();
        }
        Thread.sleep(1000);

        System.out.println(a);
    }
}
